var APP = require("../main/app")
module.exports = {
	// <div id="wrap">
	// 	<div id='header'>
	//         <div class="back" tapmode="back-active" onclick="api.closeWin()" >返回</div>
	//         <h1>权限管理测试</h1>
	//         <div class="adpt" ></div>
	// 	</div>
	// 	<div class='itemtitle'>一、判断权限</div>
	// 	<div class='marg'>请选择一个或者多个权限进行判断:</div>
	// 	<div class='marg'>日历&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="calendar" /></div>
	// 	<div class='marg'>相机&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="camera" /></div>
	// 	<div class='marg'>通讯录&emsp;&emsp;<input type="checkbox" name="p_list" value="contacts" /></div>
	// 	<div class='marg'>位置信息&emsp;<input type="checkbox" name="p_list" value="location" /></div>
	// 	<div class='marg'>麦克风&emsp;&emsp;<input type="checkbox" name="p_list" value="microphone" /></div>
	// 	<div class='marg'>电话&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="phone" /></div>
	// 	<div class='marg'>身体传感器<input type="checkbox" name="p_list" value="sensor" /></div>
	// 	<div class='marg'>短信&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="sms" /></div>
	// 	<div class='marg'>存储空间&emsp;<input type="checkbox" name="p_list" value="storage" /></div>
	// 	<div class="clickbtn" tapmode="active" onclick="hasPermission()" >点击开始判断</div>
	// 	<div class='itemtitle'>二、请求权限</div>
	// 	<div class='marg'>请选择一个或者多个权限进行请求:</div>
	// 	<div class='marg'>日历&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="calendar" /></div>
	// 	<div class='marg'>相机&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="camera" /></div>
	// 	<div class='marg'>通讯录&emsp;&emsp;<input type="checkbox" name="p_list_r" value="contacts" /></div>
	// 	<div class='marg'>位置信息&emsp;<input type="checkbox" name="p_list_r" value="location" /></div>
	// 	<div class='marg'>麦克风&emsp;&emsp;<input type="checkbox" name="p_list_r" value="microphone" /></div>
	// 	<div class='marg'>电话&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="phone" /></div>
	// 	<div class='marg'>身体传感器<input type="checkbox" name="p_list_r" value="sensor" /></div>
	// 	<div class='marg'>短信&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="sms" /></div>
	// 	<div class='marg'>存储空间&emsp;<input type="checkbox" name="p_list_r" value="storage" /></div>
	// 	<div class="clickbtn" tapmode="active" onclick="reqPermission()" >点击开始请求</div>
	// 	<div class='itemtitle'>三、需要权限的API操作</div>
	// 	<div class='marg'>1、日历</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('calendar')" >点击操作日历</div>
	// 	<div class='marg'>2、相机</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('camera')" >点击操作照相机</div>
	// 	<div class='marg'>3、通讯录</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('contacts')" >点击操作通讯录</div>
	// 	<div class='marg'>4、位置信息</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('location')" >点击操作位置信息</div>
	// 	<div class='marg'>5、麦克风</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('microphone')" >点击操作麦克风</div>
	// 	<div class='marg'>6、电话</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('phone')" >点击操作电话</div>
	// 	<div class='marg'>7、身体传感器</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('sensor')" >点击操作身体传感器</div>
	// 	<div class='marg'>8、短信</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('sms')" >点击操作短信</div>
	// 	<div class='marg'>9、存储空间</div>
	// 	<div class="clickbtn" tapmode="active" onclick="opWithPermission('storage')" >点击操作存储空间</div>
	//     <br>
	// </div>
	//初始化
	init: function() {
		var that = this;
		var powerHtml = '<div class="powerBox hidden">' +
			'<div class="marg hidden">日历&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="calendar" /></div>' +
			'<div class="marg hidden">相机&emsp;&emsp;&emsp;<input type="checkbox"  name="p_list_r" value="camera" /></div>' +
			'<div class="marg hidden">通讯录&emsp;&emsp;<input type="checkbox"  name="p_list_r" value="contacts" /></div>' +
			'<div class="marg hidden">位置信息&emsp;<input type="checkbox"   name="p_list_r" value="location" /></div>' +
			'<div class="marg hidden">麦克风&emsp;&emsp;<input type="checkbox"  name="p_list_r" value="microphone" /></div>' +
			'<div class="marg hidden">身体传感器<input type="checkbox" name="p_list_r" value="sensor" /></div>' +
			'<div class="marg hidden">短信&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="sms" /></div>' +
			'<div class="marg hidden">存储空间&emsp;<input type="checkbox" checked="checked" name="p_list_r" value="storage" /></div>' +
			'<div class="marg hidden">电话&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="phone" /></div>' +
			'<div class="clickbtn hidden" tapmode="active" onclick="reqPermission()" >' + '点击开始请求</div>' +
			'<div class="clickbtn hidden">通知&emsp;&emsp;&emsp;<input type="checkbox" checked="checked" name="p_list_r" value="notification" /></div>' +
			'</div>'
		return powerHtml;
	},
	//验证权限
	hasPermission: function(one_per,callback) {
		var that = this;
		// 验证
		var perms = new Array();
		if (one_per) {
			perms.push(one_per);
		} else {
			var prs = document.getElementsByName("p_list_r");
			for (var i = 0; i < prs.length; i++) {
				if (prs[i].checked) {
					perms.push(prs[i].value);
				}
			}
		}
		var rets = api.hasPermission({
			list: perms
		});
		var _num = 0;
		var _granted = 0;
		var _txt = 'Whetstone需要获取';
		for (var i in rets) {
			if (!rets[i].granted) {
				_granted = _num = _num + 1;
			}
		}
		for (var i in rets) {
			if (!rets[i].granted) {
				switch (rets[i].name) {
					case 'calendar':
						_txt += '日历读写'
						break;
					case 'camera':
						_txt += '相机'
						break;
					case 'contacts':
						_txt += '通讯录'
						break;
					case 'location':
						_txt += '位置信息'
						break;
					case 'microphone':
						_txt += '麦克风'
						break;
					case 'phone':
						_txt += '直接拨打电话'
						break;
					case 'sensor':
						_txt += '身体传感器'
						break;
					case 'sms':
						_txt += '发送短信'
						break;
					case 'storage':
						_txt += '存储空间'
						break;
					case 'notification':
						_txt += '状态栏通知'
						break;
					default:
						break;
				}
				_num = _num - 1;
				if (_num > 0) {
					_txt += '、'
				}
			}
		}
		_txt += '权限才能正常使用功能';
		if (_granted > 0) {
			if (!one_per) {
				that.reqPermission(one_per,callback);
			} else {
				that.reqPermission(one_per,callback);
			}
			// api.alert({
			// 	msg:_txt
			// }, function(ret, err) {
			// 	if (!one_per) {
			// 		that.reqPermission();
			// 	} else{
			// 		that.reqPermission(one_per);
			// 	}
			// });
		}else{
			//已获得权限
			$.isFunction(callback) && callback(rets);
		}
		if (!one_per) {
			// $.isFunction(callback) && callback(rets);
			// console.log('判断结果：' + JSON.stringify(rets));
			return;
		}
		
		
		return rets;
	},

	// 获取权限
	reqPermission: function(one_per, callback) {
		var that = this;
		var perms = new Array();

		if (one_per) {
			perms.push(one_per);
		} else {
			var prs = document.getElementsByName("p_list_r");
			for (var i = 0; i < prs.length; i++) {
				if (prs[i].checked) {
					perms.push(prs[i].value);
				}
			}
		}
		api.requestPermission({
			list: perms,
			code: 100001
		}, function(ret, err) {
			
			if (callback) {
				$.isFunction(callback) && callback(ret);
				return;
			}

			// that.hasPermission();
			// api.confirm({
			// 	title: 'testtitle',
			// 	msg: 'testmsg',
			// 	buttons: ['确定', '取消']
			// }, function(ret, err) {
			// 	var index = ret.buttonIndex;
			// });
			// api.alert({
			// 	msg:JSON.stringify(ret)
			// });
			// var str = '请求结果：\n';
			// str += '请求码: ' + ret.code + '\n';
			// str += "是否勾选\"不再询问\"按钮: " + (ret.never ? '是' : '否') + '\n';
			// str += '请求结果: \n';
			// var list = ret.list;
			// for (var i in list) {
			// 	str += list[i].name + '=' + list[i].granted + '\n';
			// }
			// console.log('获取权限：'+JSON.stringify(ret));
			// console.log(JSON.stringify(ret));
		});
	},
	// 获取单个权限
	opWithPermission: function(perm) {
		var that = this;
		that.hasPermission(perm);
		// if (!that.confirmPer(perm)) {
		// 	return;
		// }
		if ('calendar' == perm) {
			//操作日历
		} else if ('camera' == perm) {
			// api.getPicture({
			// 	sourceType: 'camera',
			// 	mediaValue: 'pic',
			// 	destinationType: 'url',
			// }, function(ret, err) {
			// 	if (callback) {
			// 		callback(ret);
			// 		return;
			// 	}
			// 	if (ret) {
			// 		console.log(JSON.stringify(ret));
			// 	} else {
			// 		console.log(JSON.stringify(err));
			// 	}
			// });
		} else if ('contacts' == perm) {
			api.openContacts({
				test: true
			}, function(ret, err) {
				if (ret && ret.status) {
					// console.log(JSON.stringify(ret));
				} else {
					// console.log(JSON.stringify(err));
				}
			});
		} else if ('location' == perm) {
			api.getLocation(function(ret, err) {
				if (ret && ret.status) {
					// console.log(JSON.stringify(ret));
				} else {
					// console.log(JSON.stringify(err));
				}
			});
		} else if ('microphone' == perm) {
			api.startRecord({
				path: 'fs://perm-test.amr'
			});
		} else if ('phone' == perm) {
			api.call({
				type: 'tel',
				number: '10086'
			});
		} else if ('sensor' == perm) {
			//操作身体传感器
		} else if ('sms' == perm) {
			api.sms({
				numbers: ['10086'],
				text: '余额',
				silent: true
			});
		} else if ('storage' == perm) {
			api.readFile({
				path: 'fs://test.txt'
			}, function(ret, err) {
				if (ret.status) {
					// console.log('readFile: ' + ret.data);
				} else {
					// console.log(err.msg + ": \n" + api.fsDir);
				}
			});
		}
	},

	confirmPer: function(perm) {
		var that = this;
		var has = that.hasPermission(perm);
		if (!has || !has[0] || !has[0].granted) {
			api.confirm({
				title: '提醒',
				msg: '没有获得 ' + perm + " 权限\n是否前往设置？",
				buttons: ['去设置', '取消']
			}, function(ret, err) {
				if (1 == ret.buttonIndex) {
					that.reqPermission(perm);
				}
			});
			return false;
		}
		return true;
	}

}
